<?php
/**
 * Created by Jasmine.
 * Date: 2016/9/14
 * description：小编/达人数据统计
 * Address: 浙江宣逸网络科技有限公司
 */

namespace Common\Model;


class EditorModel extends BaseModel{
    //用户标签
    const USER_TAG_USER             = 1;//普通用户
    const USER_TAG_SELLER           = 2;//商家用户
    const USER_TAG_FOOD_DAREN       = 101;//美食达人
    const USER_TAG_TRIP_DAREN       = 102;//旅游达人
    const USER_TAG_CRAFT_DAREN      = 103;//手艺达人
    const USER_TAG_FAD_DAREN        = 104;//时尚达人
    const USER_TAG_FILM_DAREN       = 105;//影视达人
    const USER_TAG_OUTDOOR_DAREN    = 106;//户外达人
    const USER_TAG_PUBLIC_DAREN     = 107;//公益达人
    const USER_TAG_FOOD_EDITOR      = 501;//美食小编
    const USER_TAG_TRIP_EDITOR      = 502;//旅游小编
    const USER_TAG_CRAFT_EDITOR     = 503;//手艺小编
    const USER_TAG_FAD_EDITOR       = 504;//时尚小编
    const USER_TAG_FILM_EDITOR      = 505;//影视小编
    const USER_TAG_OUTDOOR_EDITOR   = 506;//户外小编
    const USER_TAG_PUBLIC_EDITOR    = 507;//公益小编

    //类型(同cfg_content_type)
    const TYPE_OF_FOOD              = 1;//美食
    const TYPE_OF_TRIP              = 2;//旅游
    const TYPE_OF_CRAFT             = 3;//手艺
    const TYPE_OF_FAD               = 4;//时尚
    const TYPE_OF_FILM              = 5;//影视
    const TYPE_OF_OUTDOOR           = 6;//户外
    const TYPE_OF_PUBLIC            = 16;//公益

    //数据时间段
    const DATE_YESTERDAY           =1;//昨天
    const DATE_LAST_WEEK           =2;//上周
    const DATE_LAST_MONTH          =3;//上个月

    //商务评价
    const COMMERCE_EVALUATE_ON      =0;//已评价
    const COMMERCE_EVALUATE_OFF     =1;//未评价

//    private static function getEditorTag(){
//        return array(
//            '美食'    => self::USER_TAG_FOOD_EDITOR,
//            '旅游'    => self::USER_TAG_TRIP_EDITOR,
//            '手艺'    => self::USER_TAG_CRAFT_EDITOR,
//            '时尚'    => self::USER_TAG_FAD_EDITOR,
//            '影视'    => self::USER_TAG_FILM_EDITOR,
//            '户外'    => self::USER_TAG_OUTDOOR_EDITOR,
//        );
//    }
    public static function getDaRenTag(){
        return array(
            '美食'    => self::USER_TAG_FOOD_DAREN,
            '旅游'    => self::USER_TAG_TRIP_DAREN,
            '手艺'    => self::USER_TAG_CRAFT_DAREN,
            '时尚'    => self::USER_TAG_FAD_DAREN,
            '影视'    => self::USER_TAG_FILM_DAREN,
            '户外'    => self::USER_TAG_OUTDOOR_DAREN,
            '公益'    => self::USER_TAG_PUBLIC_DAREN,
        );
    }

    public static function userTagGetType(){
        return array(
            self::USER_TAG_FOOD_EDITOR     => self::TYPE_OF_FOOD,
            self::USER_TAG_TRIP_EDITOR     => self::TYPE_OF_TRIP,
            self::USER_TAG_CRAFT_EDITOR    => self::TYPE_OF_CRAFT,
            self::USER_TAG_FAD_EDITOR      => self::TYPE_OF_FAD,
            self::USER_TAG_FILM_EDITOR     => self::TYPE_OF_FILM,
            self::USER_TAG_OUTDOOR_EDITOR  => self::TYPE_OF_OUTDOOR,
            self::USER_TAG_PUBLIC_EDITOR   => self::TYPE_OF_PUBLIC,
        );
    }

    public static function daRenTagGetType(){
        return array(
            self::USER_TAG_FOOD_DAREN     => self::TYPE_OF_FOOD,
            self::USER_TAG_TRIP_DAREN     => self::TYPE_OF_TRIP,
            self::USER_TAG_CRAFT_DAREN    => self::TYPE_OF_CRAFT,
            self::USER_TAG_FAD_DAREN      => self::TYPE_OF_FAD,
            self::USER_TAG_FILM_DAREN     => self::TYPE_OF_FILM,
            self::USER_TAG_OUTDOOR_DAREN  => self::TYPE_OF_OUTDOOR,
            self::USER_TAG_PUBLIC_DAREN   => self::TYPE_OF_PUBLIC,
        );
    }

    public static function editorDataStatistic($where,$time){
        $articleM = M('article',null,DB_MAIN_CFG);
        $eventM = M('e_events',null,DB_MAIN_CFG);
//        $type = M('cfg_content_type',null,DB_MAIN_CFG)->select();
        $type = M('cfg_user_tag',null,DB_MAIN_CFG)->where("tid between 500 and 600")->select();
        $getType = self::userTagGetType();
        $data = array();
        foreach($type as $key => $value){
            $addArticleNum=$praiseNum=$commentNum=$commentTimes=$replyNum=$replyRatio=$articleFromDaRen=$addEventNum=$applyNum=$selectedNum=0;
            $writeUid = M('u_user_base',null,DB_MAIN_CFG)->where("userTag={$value['tid']}")->getField("uid",true);     //作者UID
//            $editorUid = M('gm_sys_user',null,DB_GM_CFG)->where("role_id=".SYSTEM_ROLE_EDITOR)->field("id,uid")->select();         //发稿人ID和作者UID
            if($writeUid){
                $writerStr = implode(',',$writeUid);        //作者
                $editorArr = M('gm_sys_user',null,DB_GM_CFG)->where("role_id=".SYSTEM_ROLE_EDITOR." and uid IN ({$writerStr})")->getField("id",true);
                if($editorArr){
                    $editorStr = implode(',',$editorArr);   //发稿人后台帐号ID
                }
                //文章
                $article = $articleM
                    ->where($where." and uid IN ({$writerStr}) and editor IN ({$editorStr}) and audit=".AUDIT_PASS)
                    ->field("id,uid,praiseNum")
                    ->select();
                $addArticleNum = count($article);       //添加文章数
                if($article){
                    $praiseNum=0;$aidArr=array();
                    foreach($article as $num){
                        $praiseNum += $num['praiseNum'];    //点赞人数
                        $aidArr[] = $num['id'];
                    }
                    $aidStr = implode(',',$aidArr);
                    $articleCommentM = M('comment_1',null,DB_MAIN_CFG);

                    $articleCommentNum = $articleCommentM
                        ->where($where." and objID in ({$aidStr}) and uid NOT IN ({$writerStr})")
                        ->count('distinct uid,objID');   //文章评论人数
                    $articleCommentNum = key($articleCommentNum);
                    $articleCommentTimes = $articleCommentM
                        ->where($where." and uid NOT IN ({$writerStr}) and objID in ({$aidStr})")
                        ->count();   //文章评论次数
                    $articleReplyNum = $articleCommentM
                        ->where($where." and uid in ({$writerStr}) and parentID > 0 and objID in ({$aidStr})")
                        ->count('distinct uid,objID');  //文章回复数
                    $articleReplyNum = key($articleReplyNum);

                } else{
                    $articleCommentNum = $articleCommentTimes = $articleReplyNum =0;
                }
                //帮达人发稿
                $daRenUid = M('u_user_base',null,DB_MAIN_CFG)->where("userTag between 100 and 200")->getField("uid",true);
                if($daRenUid){
                    $daRenUidStr = implode(',',$daRenUid);
                    $articleFromDaRen = $articleM
                        ->where($where." and uid IN ({$daRenUidStr}) and editor IN ({$editorStr}) and clientFrom=".CLIENT_FROM_ADMIN." and audit=".AUDIT_PASS)
                        ->count();
                }else{
                    $articleFromDaRen =0;
                }
                //活动
                $event = $eventM
                    ->where($where." and editor IN ({$editorStr}) and del=".EventsModel::EVENTS_STATUS_ON)
                    ->field("id,applyCount")
                    ->select();
                $addEventNum = count($event);   //新增活动数
                if($event){
                    $applyNum = 0;$eventId = array();
                    foreach($event as $val){
                        $applyNum += $val['applyCount'];    //报名人数
                        $eventId[] = $val['id'];
                    }
                    $eidStr = implode(',',$eventId);
                    $eventCommentM = M('comment_2',null,DB_MAIN_CFG);
                    $eventCommentNum = $eventCommentM
                        ->where($where." and objID IN ({$eidStr}) and uid NOT IN ({$writerStr})")
                        ->count("distinct uid,objID");  //活动评论人数
                    $eventCommentNum = key($eventCommentNum);
                    $eventCommentTimes = $eventCommentM
                        ->where($where." and objID IN ({$eidStr}) and uid NOT IN ({$writerStr})")
                        ->count();  //活动评论次数
                    $eventReplyNum = $eventCommentM
                        ->where($where." and uid IN ({$writerStr}) and parentID > 0 and objID in ({$eidStr})")
                        ->count('distinct uid,objID');  //活动回复数
                    $eventReplyNum = key($eventReplyNum);
                    $selectedNum = M('e_events_join',null,DB_MAIN_CFG)
                        ->where("eid IN ({$eidStr}) and status=".EventsModel::JOIN_STATUS_SELECTED." and del=".EventsModel::EVENTS_STATUS_ON)
                        ->count();  //已入选人数
                } else{
                    $eventCommentNum = $eventCommentTimes = $eventReplyNum = $selectedNum =0;
                }

                $commentNum = $articleCommentNum + $eventCommentNum;
                $commentTimes = $articleCommentTimes + $eventCommentTimes;
                $replyNum = $articleReplyNum + $eventReplyNum;
                if($commentNum > 0){
                    $replyRatio = $replyNum/$commentNum;
                } else{
                    $replyRatio = 0;
                }
            }
            $data[$key] = array(
                'objTime'           => $time,
                'type'              => $getType[$value['tid']],
                'addArticleNum'     => $addArticleNum,
                'praiseNum'         => $praiseNum,
                'commentNum'        => $commentNum,
                'commentTimes'      => $commentTimes,
                'replyNum'          => $replyNum,
                'replyRatio'        => $replyRatio,
                'articleFromDaRen'  => $articleFromDaRen,
                'addEventNum'       => $addEventNum,
                'applyNum'          => $applyNum,
                'selectedNum'       => $selectedNum
            );
        }
        return $data;
    }



    //统计各类编辑日数据
    public static function editDayDataStatistic(){
        $yesterday = UtilsModel::getYesterDay();
        $where = "1 and addTime between {$yesterday[0]} and {$yesterday[1]}";
        //数据对象时间（12点）
        $objTime = STATISTIC_DAILY_OBJTIME;
        $data = self::editorDataStatistic($where,$objTime);
        M('editor_day_data',null,DB_MAIN_CFG)->addAll($data);
    }

    //统计各类编辑周数据
    public static function editWeekDataStatistic(){
        $lastWeekTime = UtilsModel::getTimeWeek(time());
        $where = "1 and addTime between {$lastWeekTime[0]} and {$lastWeekTime[1]}";
        //数据对象时间(周三)
        $objTime = STATISTIC_WEEKLY_OBJTIME;
        $data = self::editorDataStatistic($where,$objTime);
        M('editor_week_data',null,DB_MAIN_CFG)->addAll($data);
    }

    //统计各类编辑月数据
    public static function editMonthDataStatistic(){
        $lastMonTime = UtilsModel::getLastMonth();
        $where = "1 and addTime between {$lastMonTime[0]} and {$lastMonTime[1]}";
        //数据对象时间(15号)
        $objTime = STATISTIC_MONTHLY_OBJTIME;
        $data = self::editorDataStatistic($where,$objTime);
        M('editor_month_data',null,DB_MAIN_CFG)->addAll($data);
    }

    //统计各个编辑月排行
    public static function everyEditorMonRank(){
        $lastMonTime = UtilsModel::getLastMonth();
        $where = "1 and addTime between {$lastMonTime[0]} and {$lastMonTime[1]}";
        //数据对象时间(15号)
        $objTime = mktime(0,0,0,date('m')-1,15,date('Y'));

        $articleM = M('article',null,DB_MAIN_CFG);
        $eventM = M('e_events',null,DB_MAIN_CFG);
        $data = array();
        $allEditor = M('gm_sys_user',null,DB_GM_CFG)->where("role_id=".SYSTEM_ROLE_EDITOR)->field("id,uid")->select();
        $getType = self::userTagGetType();
        if($allEditor){
            foreach($allEditor as $key=>$value){
                $typeAndName= M('u_user_base',null,DB_MAIN_CFG)->where("uid={$value['uid']}")->field('realName,userTag')->find();
                $praiseNum=$commentNum=$commentTimes=$replyNum=$replyRatio=$applyNum=0;
                //文章
                $article = $articleM
                    ->where($where." and uid={$value['uid']} and audit=".AUDIT_PASS)
                    ->field("id,praiseNum")
                    ->select();
                $addArticleNum = count($article);       //添加文章数
                if($article){
                    $praiseNum=0;$aidArr=array();
                    foreach($article as $num){
                        $praiseNum += $num['praiseNum'];    //点赞人数
                        $aidArr[] = $num['id'];
                    }
                    $aidStr = implode(',',$aidArr);
                    $articleCommentM = M('comment_1',null,DB_MAIN_CFG);
                    $articleCommentNum = $articleCommentM
                        ->where($where." and objID in ({$aidStr}) and uid != {$value['uid']}")
                        ->count('distinct uid,objID');   //文章评论人数
                    $articleCommentNum = key($articleCommentNum);
                    $articleCommentTimes = $articleCommentM
                        ->where($where." and uid != {$value['uid']} and objID in ({$aidStr})")
                        ->count();   //文章评论次数
                    $articleReplyNum = $articleCommentM
                        ->where($where." and uid = {$value['uid']} and parentID > 0 and objID in ({$aidStr})")
                        ->count('distinct uid,objID');  //文章回复数
                    $articleReplyNum = key($articleReplyNum);
                } else{
                    $articleCommentNum = $articleCommentTimes = $articleReplyNum =0;
                }

                //帮达人发稿
                $daRenUid = M('u_user_base',null,DB_MAIN_CFG)->where("userTag between 100 and 200")->getField("uid",true);
                if($daRenUid){
                    $daRenUidStr = implode(',',$daRenUid);
                    $articleFromDaRen = $articleM
                        ->where($where." and uid IN ({$daRenUidStr}) and editor={$value['id']} and clientFrom=".CLIENT_FROM_ADMIN." and audit=".AUDIT_PASS)
                        ->count();
                } else{
                    $articleFromDaRen =0;
                }

                //活动
                $event = $eventM
                    ->where($where." and editor={$value['id']} and del=".EventsModel::EVENTS_STATUS_ON)
                    ->field("id,applyCount")
                    ->select();
                $addEventNum = count($event);   //新增活动数
                if($event){
                    $applyNum = 0;$eventId = array();
                    foreach($event as $v){
                        $applyNum += $v['applyCount'];    //报名人数
                        $eventId[] = $v['id'];
                    }
                    $eidStr = implode(',',$eventId);
                    $eventCommentM = M('comment_2',null,DB_MAIN_CFG);
                    $eventCommentNum = $eventCommentM
                        ->where($where." and objID IN ({$eidStr}) and uid != {$value['uid']}")
                        ->count('distinct uid,objID');  //活动评论人数
                    $eventCommentNum = key($eventCommentNum);
                    $eventCommentTimes = $eventCommentM
                        ->where($where." and objID IN ({$eidStr}) and uid != {$value['uid']}")
                        ->count();  //活动评论次数
                    $eventReplyNum = $eventCommentM
                        ->where($where." and uid = {$value['uid']} and parentID > 0 and objID in ({$eidStr})")
                        ->count('distinct uid,objID');  //活动回复数
                    $eventReplyNum = key($eventReplyNum);
                    $selectedNum = M('e_events_join',null,DB_MAIN_CFG)
                        ->where("eid IN ({$eidStr}) and status=".EventsModel::JOIN_STATUS_SELECTED." and del=".EventsModel::EVENTS_STATUS_ON)
                        ->count();  //已入选人数
                } else{
                    $eventCommentNum = $eventCommentTimes = $eventReplyNum = $selectedNum = 0;
                }

                $commentNum = $articleCommentNum + $eventCommentNum;
                $commentTimes = $articleCommentTimes + $eventCommentTimes;
                $replyNum = $articleReplyNum + $eventReplyNum;
                if($commentNum > 0){
                    $replyRatio = $replyNum/$commentNum;
                } else{
                    $replyRatio = 0;
                }

                $data[$key] = array(
                    'objTime'           => $objTime,
                    'uid'               => $value['uid'],
                    'realName'          => $typeAndName['realName'],
                    'type'              => $getType[$typeAndName['userTag']],
                    'addArticleNum'     => $addArticleNum,
                    'praiseNum'         => $praiseNum,
                    'commentNum'        => $commentNum,
                    'commentTimes'      => $commentTimes,
                    'replyNum'          => $replyNum,
                    'replyRatio'        => $replyRatio,
                    'articleFromDaRen'  => $articleFromDaRen,
                    'addEventNum'       => $addEventNum,
                    'applyNum'          => $applyNum,
                    'selectedNum'       => $selectedNum
                );
            }
            M('editor_data_rank',null,DB_MAIN_CFG)->addAll($data);
        }
    }

    public static function daRenDataStatistic($where,$objTime,$dataType){
        $articleM = M('article',null,DB_MAIN_CFG);
        $commentM = M('comment_1',null,DB_MAIN_CFG);
        $userM = M('u_user_base',null,DB_MAIN_CFG);
//        $type = M('cfg_content_type',null,DB_MAIN_CFG)->select();
        $type = M('cfg_user_tag',null,DB_MAIN_CFG)->where("tid between 100 and 200")->select();
        $getType = self::daRenTagGetType();
        $data = array();
        foreach($type as $key => $value){
            $daRenUid = $userM->where("userTag={$value['tid']}")->getField("uid",true);
            $daRenNum = count($daRenUid);   //达人总数
            switch($dataType){
                case 'day':
                    $lastDaRenNum = M('expert_day_data',null,DB_MAIN_CFG)->where("type={$getType[$value['tid']]}")->order('objTime DESC')->getField('daRenNum');
                    break;
                case 'week':
                    $lastDaRenNum = M('expert_week_data',null,DB_MAIN_CFG)->where("type={$getType[$value['tid']]}")->order('objTime DESC')->getField('daRenNum');
                    break;
                case 'month':
                    $lastDaRenNum = M('expert_month_data',null,DB_MAIN_CFG)->where("type={$getType[$value['tid']]}")->order('objTime DESC')->getField('daRenNum');
                    break;
                default:
                    $lastDaRenNum = 0;
                    break;
            }
            $addDaRenNum = $daRenNum-$lastDaRenNum;     //新增达人数量
            if($daRenUid){
                $daRenUidStr = implode(',',$daRenUid);
                $article = $articleM
                    ->where($where." and uid IN ({$daRenUidStr}) and audit=".AUDIT_PASS)
                    ->field("id,praiseNum")
                    ->select();
                $addArticleNum = count($article);   //投稿数
                $praiseNum = 0;
                $aid=array();
                if($article){
                    foreach($article as $val){
                        $praiseNum += $val['praiseNum'];     //点赞人数
                        $aid[] = $val['id'];
                    }
                    $aidStr = implode(',',$aid);
                    $commentNum = $commentM     //评论人数
                        ->where($where." and objID IN ({$aidStr}) and uid NOT IN ({$daRenUidStr})")
                        ->count('distinct uid,objID');
                    $commentNum = key($commentNum);
                    $commentTimes = $commentM   //评论次数
                        ->where($where." and objID IN ({$aidStr}) and uid NOT IN ({$daRenUidStr})")
                        ->count();
                    $replyNum = $commentM       //回复数
                        ->where($where." and uid IN ({$daRenUidStr}) and parentID > 0 and objID IN ({$aidStr})")
                        ->count('distinct uid,objID');
                    $replyNum = key($replyNum);
                }else{
                    $commentNum = $commentTimes = $replyNum =0;
                }
            }else{
                $addArticleNum = $praiseNum = $commentNum = $commentTimes = $replyNum =0;
            }

            $data[$key] = array(
                'objTime'       => $objTime,
                'type'          => $getType[$value['tid']],
                'daRenNum'      => $daRenNum,
                'addDaRenNum'   => $addDaRenNum,
                'addArticleNum' => $addArticleNum,
                'praiseNum'     => $praiseNum,
                'commentNum'    => $commentNum,
                'commentTimes'  => $commentTimes,
                'replyNum'      => $replyNum,
            );
        }
        return $data;
    }



    //统计各类达人日数据
    public static function daRenDayDataStatistic(){
        $yesterday = UtilsModel::getYesterDay();
        $where = "1 and addTime between {$yesterday[0]} and {$yesterday[1]}";
        //数据对象时间（12点）
        $objTime = mktime(12,0,0,date('m'),date('d')-1,date('Y'));
        $data = self::daRenDataStatistic($where,$objTime,'day');
        M('expert_day_data',null,DB_MAIN_CFG)->addAll($data);
    }

    //统计各类达人周数据
    public static function daRenWeekDataStatistic(){
        $lastWeekTime = UtilsModel::getTimeWeek(time());
        $where = "1 and addTime between {$lastWeekTime[0]} and {$lastWeekTime[1]}";
        //数据对象时间(周三)
        $objTime = mktime(0,0,0,date('m'),date('d')-date('w')+1-5,date('Y'));

        $data = self::daRenDataStatistic($where,$objTime,'week');
        M('expert_week_data',null,DB_MAIN_CFG)->addAll($data);
    }

    //统计各类达人月数据
    public static function daRenMonthDataStatistic(){
        $lastMonTime = UtilsModel::getLastMonth();
        $where = "1 and addTime between {$lastMonTime[0]} and {$lastMonTime[1]}";
        //数据对象时间(15号)
        $objTime = mktime(0,0,0,date('m')-1,15,date('Y'));

        $data = self::daRenDataStatistic($where,$objTime,'month');
        M('expert_month_data',null,DB_MAIN_CFG)->addAll($data);
    }

    //统计各个达人月数据
    public static function everyDaRenMonRank(){
        $lastMonTime = UtilsModel::getLastMonth();
        $where = "1 and addTime between {$lastMonTime[0]} and {$lastMonTime[1]}";
        //数据对象时间(15号)
        $objTime = mktime(0,0,0,date('m')-1,15,date('Y'));

        $articleM = M('article',null,DB_MAIN_CFG);
        $commentM = M('comment_1',null,DB_MAIN_CFG);
        $userM = M('u_user_base',null,DB_MAIN_CFG);
        $getType = self::daRenTagGetType();
        $data = array();
        $daRenInfo = $userM->where("userTag between 100 and 200")->field('uid,realName,userTag')->select();
        if($daRenInfo){
            foreach($daRenInfo as $key=>$value){
                $article = $articleM
                    ->where($where." and uid={$value['uid']} and audit=".AUDIT_PASS)
                    ->field("id,praiseNum")
                    ->select();
                $addArticleNum = count($article);   //投稿数
                $praiseNum = 0;
                $aid=array();
                if($article){
                    foreach($article as $val){
                        $praiseNum += $val['praiseNum'];     //点赞人数
                        $aid[] = $val['id'];
                    }
                    $aidStr = implode(',',$aid);
                    $commentNum = $commentM     //评论人数
                        ->where($where." and objID IN ({$aidStr}) and uid != {$value['uid']}")
                        ->count('distinct uid,objID');
                    $commentNum = key($commentNum);
                    $commentTimes = $commentM   //评论次数
                        ->where($where." and objID IN ({$aidStr}) and uid != {$value['uid']}")
                        ->count();
                    $replyNum = $commentM       //回复数
                        ->where($where." and uid = {$value['uid']} and parentID > 0 and objID IN ({$aidStr})")
                        ->count('distinct uid,objID');
                    $replyNum = key($replyNum);
                }else{
                    $commentNum = $commentTimes = $replyNum =0;
                }
                $data[$key] = array(
                    'objTime'       => $objTime,
                    'type'          => $getType[$value['userTag']],
                    'uid'           => $value['uid'],
                    'realName'      => $value['realName'],
                    'addArticleNum' => $addArticleNum,
                    'praiseNum'     => $praiseNum,
                    'commentNum'    => $commentNum,
                    'commentTimes'  => $commentTimes,
                    'replyNum'      => $replyNum,
                );
            }
        }
        M('expert_data_rank',null,DB_MAIN_CFG)->addAll($data);
    }

    //获取类型
    public static function getType($id=0){
        $where = "1";
        if($id){
            $where = "id = {$id}";
        }
        $type = M('cfg_content_type',null,DB_MAIN_CFG)->where($where)->getField("name",true);
        return $type;
    }

    //Wap读取日数据
    public static function getDayData(){
        $yesterday = UtilsModel::getYesterDay();
        $where = "1 and objTime between {$yesterday[0]} and {$yesterday[1]}";
        $editorData = M('editor_day_data',null,DB_MAIN_CFG)->where($where)->select();
        foreach($editorData as $key => $val){
            $editorData[$key]['typeName'] = self::getType($val['type']);
        }
        $expertData = M('expert_day_data',null,DB_MAIN_CFG)->where($where)->select();
        foreach($expertData as $key => $val){
            $expertData[$key]['typeName'] = self::getType($val['type']);
        }
        $data = array('editor'=>$editorData,'expert'=>$expertData);
        return $data;
    }


    //Wap读取周数据
    public static function getWeekData(){
        $lastWeekTime = UtilsModel::getTimeWeek(time());
        $where = "1 and objTime between {$lastWeekTime[0]} and {$lastWeekTime[1]}";
        $editorData = M('editor_week_data',null,DB_MAIN_CFG)->where($where)->select();
        foreach($editorData as $key => $val){
            $editorData[$key]['typeName'] = self::getType($val['type']);
        }
        $expertData = M('expert_week_data',null,DB_MAIN_CFG)->where($where)->select();
        foreach($expertData as $key => $val){
            $expertData[$key]['typeName'] = self::getType($val['type']);
        }
        $data = array('editor'=>$editorData,'expert'=>$expertData);
        return $data;
    }

    //Wap读取月数据
    public static function getMonData(){
        $lastMonTime = UtilsModel::getLastMonth();
        $where = "1 and objTime between {$lastMonTime[0]} and {$lastMonTime[1]}";
        $editorData = M('editor_month_data',null,DB_MAIN_CFG)->where($where)->select();
        foreach($editorData as $key => $val){
            $editorData[$key]['typeName'] = self::getType($val['type']);
        }
        $expertData = M('expert_month_data',null,DB_MAIN_CFG)->where($where)->select();
        foreach($expertData as $key => $val){
            $expertData[$key]['typeName'] = self::getType($val['type']);
        }
        $data = array('editor'=>$editorData,'expert'=>$expertData);
        return $data;
    }

    //保存小编录入信息
    public static function saveEditorWriteData($uid=0,$friendNum=0,$groupFriend=0,$newFriendNum=0){
        $dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
        $dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
        if(!$uid){
            $dataFailure['msg'] = "请先登录…";
            return $dataFailure;
        }
        $userRole = UserModel::getUserRole($uid);
        if($userRole != SYSTEM_ROLE_EDITOR) {
            $dataFailure['msg'] = "这是小编操作，您不是小编哦";
            return $dataFailure;
        }
        $time = time();
        $type = EditorModel::userTagGetType();
        $userTag = M('u_user_base',null,DB_MAIN_CFG)->where("uid={$uid}")->getField("userTag");
        $objType = $type[$userTag];

        $data = array(
            'uid'           => $uid,
            'addTime'       => $time,
            'type'          => $objType,
            'friendNum'     => $friendNum,
            'groupFriend'   => $groupFriend,
            'newFriendNum'  => $newFriendNum
        );

        $writeM = M('weixin_friend_write',null,DB_MAIN_CFG);

        $todayTime = UtilsModel::getYesterDay();
        $where = "uid={$uid} and addTime between $todayTime[0] and $todayTime[1]";
        if($writeM->where($where)->find()){
            $writeM->where($where)->save($data);
        }

        $id = $writeM->add($data);
        if($id){
            return $dataSuccess;
        } else{
            return $dataFailure;
        }
    }

    //小编查看录入信息
    public static function getEditorWriteData($uid){
        $time=time();
        $startTime = mktime(0,0,0,date("m",$time),date("d",$time),date("Y",$time));
        $endTime = mktime(23,59,59,date("m",$time),date("d",$time),date("Y",$time));
        $where = "uid={$uid} and addTime between {$startTime} and {$endTime}";
        $data = M('weixin_friend_write',null,DB_MAIN_CFG)->where($where)->find();
        return $data;
    }

    //统计小编微信好友数据
    public static function editorFriendsStatistic(){
        $time = UtilsModel::getYesterDay();
        $where = "addTime between {$time[0]} and {$time[1]}";
        //数据对象时间（12点）
        $objTime = STATISTIC_DAILY_OBJTIME;

        $data = M('weixin_friend_write',null,DB_MAIN_CFG)
            ->where($where)
            ->field("SUM(friendNum) as friendNum,SUM(groupFriend) as groupFriendNum,type")
            ->group("type")
            ->select();

        $arr = array();
        $arr['totalFriendNum'] = 0;
        if($data){
            foreach($data as  $v){
                if($v['type'] == self::TYPE_OF_FOOD){
                    $arr['foodFriendNum'] = $v['friendNum'];
                    $arr['foodGroupFriendNum'] = $v['groupFriendNum'];
                    $arr['totalFriendNum'] += $v['friendNum'];
                } else if($v['type'] == self::TYPE_OF_TRIP){
                    $arr['tripFriendNum'] = $v['friendNum'];
                    $arr['tripGroupFriendNum'] = $v['groupFriendNum'];
                    $arr['totalFriendNum'] += $v['friendNum'];
                } else if($v['type'] == self::TYPE_OF_CRAFT){
                    $arr['craftFriendNum'] = $v['friendNum'];
                    $arr['craftGroupFriendNum'] = $v['groupFriendNum'];
                    $arr['totalFriendNum'] += $v['friendNum'];
                } else if($v['type'] == self::TYPE_OF_FAD){
                    $arr['fadFriendNum'] = $v['friendNum'];
                    $arr['fadGroupFriendNum'] = $v['groupFriendNum'];
                    $arr['totalFriendNum'] += $v['friendNum'];
                } else if($v['type'] == self::TYPE_OF_FILM){
                    $arr['filmFriendNum'] = $v['friendNum'];
                    $arr['filmGroupFriendNum'] = $v['groupFriendNum'];
                    $arr['totalFriendNum'] += $v['friendNum'];
                } else if($v['type'] == self::TYPE_OF_OUTDOOR){
                    $arr['outdoorFriendNum'] = $v['friendNum'];
                    $arr['outdoorGroupFriendNum'] = $v['groupFriendNum'];
                    $arr['totalFriendNum'] += $v['friendNum'];
                }
            }
        }

        $arr['objTime'] = $objTime;
        M('weixin_friend_statistic',null,DB_MAIN_CFG)->add($arr);
    }

    //商务评价
    public static function businessManagerEvaluate($uid,$id,$attitude,$speed,$environment,$content){
        $dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
        $dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');

        if(!$uid){
            $dataFailure['msg']="请先登录…";
            return $dataFailure;
        }
        if(!$id){
            return $dataFailure;
        }
        $where = "id={$id}";
        $evaluateM = M('commerce_evaluate',null,DB_MAIN_CFG);
        $objInfo = $evaluateM->where($where)->find();
        if(!$objInfo){
            $dataFailure['msg']='指定文件不存在';
            return $dataFailure;
        }

        $score = $attitude+$speed+$environment;
        $obj = array(
            'evaluateTime'  => time(),
            'attitudeScore' => $attitude,
            'speedScore'    => $speed,
            'environmentScore'=> $environment,
            'score'         => $score,
            'content'       => $content,
            'status'        => self::COMMERCE_EVALUATE_ON,
        );

        $result = $evaluateM->where($where)->save($obj);
        if($result !== false){
            return $dataSuccess;
        } else{
            $dataFailure['msg']='评价失败，请重试';
            return $dataFailure;
        }
    }

    //商务评价列表
    public static function evaluateList($uid){
        $data = M('commerce_evaluate',null,DB_MAIN_CFG)
            ->where("commerceID={$uid}")
            ->field("id,objID,title,objType,status")
            ->order("id DESC")
            ->select();
        foreach($data as $key => $val){
            if($val['objType'] == OBJ_TYPE_ARTICLE){
                $more = M('article',null,DB_MAIN_CFG)->where("id={$val['objID']}")->field("listPic,addTime")->find();
                $data[$key]['listPic'] = $more['listPic'];
                $data[$key]['addTime'] = $more['addTime'];
                $data[$key]['type'] = "文章";
            }else if($val['objType'] == OBJ_TYPE_ACTIVITY){
                $more = M('e_events',null,DB_MAIN_CFG)->where("id={$val['objID']}")->field("imgList,addTime")->find();
                $data[$key]['listPic'] = $more['imgList'];
                $data[$key]['addTime'] = $more['addTime'];
                $data[$key]['type'] = "活动";
            }
        }
        return $data;
    }

    //评价界面
    public static function evaluatePage($id){
        $data = M('commerce_evaluate',null,DB_MAIN_CFG)->where("id={$id}")->field("id,attitudeScore,speedScore,environmentScore,content")->find();
        return $data;
    }

}

